﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.DBC;
using Styx.WoWInternals.WoWObjects;

namespace WuzzMonk
{
    public partial class Monk : CombatRoutine
    {
        #region Def CDs
        public bool CastDefensiveSpells()
        {
            // Fortifying Brew if under 75% HP
            if (Me.HealthPercent < 75)
                if (CastBuff("Fortifying Brew", null)) { return true; }

            // Touch of Karma if possible. HP under 75%
            if (Me.HealthPercent <= 50 && Me.CurrentTarget.IsWithinMeleeRange)
                if (CastSpecial("Touch of Karma", Me.CurrentTarget)) { return true; }

            return false;
        }
        #endregion

        #region HP Reg Abilities
        private bool CastHeal()
        {
            // Chi Wave
            if (Me.HealthPercent <= 75 && !Cooldown("Chi Wave") && Me.CurrentChi >= 3 && Me.CurrentTarget.IsWithinMeleeRange)
            {
                if (CastHeal("Chi Wave", Me.CurrentTarget)) { return true; }
            }
            // Expel Harm
            if (Me.HealthPercent < 50 && !Cooldown("Expel Harm") && Me.CurrentEnergy >= 40)
            {
                if (CastHeal("Expel Harm", null)) { return true; }
            }

            return false;
        }

        private void UseHealthPotion()
        {
            if (Me.HealthPercent <= 50)
            {
                WoWItem Potion = Helpers.Item.FindFirstUsableItemBySpell("Healing Potion");
                if (Potion.Usable)
                {
                    Helpers.Item.UseItem(Potion);
                }
            }
        }
        #endregion

        #region Interrupts
        public bool CastInterrupt()
        {
            if (Me.CurrentTarget.IsCasting && Me.CurrentTarget.CanInterruptCurrentSpellCast)
            {
                if (SpellManager.Spells.ContainsKey("Spear Hand Strike") && !Cooldown("Spear Hand Strike") && Me.CurrentEnergy >= 30 && Me.CurrentTarget.IsWithinMeleeRange)
                {
                    if (CastSpecial("Spear Hand Strike", Me.CurrentTarget)) { return true; }
                }
            }
            return false;
        }

        #endregion

        #region PreCombat Buffs
        private bool NeedLegacyOfTheEmperor()
        {
            if (SpellManager.Spells.ContainsKey("Legacy of the Emperor") && !Helpers.Unit.HasAnyAura(Me, Lists.Buffs.StatBuffs))
            {
                return true;
            }
            return false;
        }

        private void CastLegacyOfTheEmperor()
        {
            if (SpellManager.Spells.ContainsKey("Legacy of the Emperor") && !Helpers.Unit.HasAnyAura(Me, Lists.Buffs.StatBuffs))
            {
                if (CastBuff("Legacy of the Emperor", Me)) { return; }
            }
        }

        private bool NeedLegacyOfTheWhiteTiger()
        {
            if (SpellManager.Spells.ContainsKey("Legacy of the White Tiger") && !Helpers.Unit.HasAnyAura(Me, Lists.Buffs.CritBuffs))
            {
                return true;
            }
            return false;
        }

        private void CastLegacyOfTheWhiteTiger()
        {
            if (SpellManager.Spells.ContainsKey("Legacy of the White Tiger") && !Helpers.Unit.HasAnyAura(Me, Lists.Buffs.CritBuffs))
            {
                if (CastBuff("Legacy of the White Tiger", Me)) { return; }
            }
        }
        #endregion

        #region Spellcasting Methods
        private bool CastBuff(string BuffName, WoWUnit Target)
        {
            if (SpellManager.Spells.ContainsKey(BuffName) && SpellManager.CanCast(BuffName))
            {
                if (SpellManager.Cast(BuffName, Target))
                {
                    Logging.Write(Colors.LightBlue, "WuzzMonk - Buff: " + BuffName + " (Energy: " + Me.CurrentEnergy + ", Chi: " + Me.CurrentChi.ToString() + ")");
                    return true;
                }
            }
            return false;
        }

        private bool CastSpell(string SpellName, WoWUnit Target)
        {
            if (SpellManager.CanCast(SpellName) && Me.IsFacing(Me.CurrentTarget.Location))
            {
                if (SpellManager.Cast(SpellName, Target))
                {
                    Logging.Write(Colors.Red, "WuzzMonk - Attack: " + SpellName + " (Energy: " + Me.CurrentEnergy + ", Chi: " + Me.CurrentChi.ToString() + ")");
                    return true;
                }
            }
            return false;
        }

        private bool CastSpecial(string SpellName, WoWUnit Target)
        {
            if (SpellManager.CanCast(SpellName) && Me.IsFacing(Me.CurrentTarget.Location))
            {
                if (SpellManager.Cast(SpellName, Target))
                {
                    Logging.Write(Colors.DarkOrange, "WuzzMonk - Special: " + SpellName + " (Energy: " + Me.CurrentEnergy + ", Chi: " + Me.CurrentChi.ToString() + ")");
                    return true;
                }
            }
            return false;
        }

        private bool CastHeal(string SpellName, WoWUnit Target)
        {
            if (SpellManager.CanCast(SpellName) && Me.IsFacing(Me.CurrentTarget.Location))
            {
                if (SpellManager.Cast(SpellName, Target))
                {
                    Logging.Write(Colors.LightGreen, "WuzzMonk - Heal: " + SpellName + " (Energy: " + Me.CurrentEnergy + ", Chi: " + Me.CurrentChi.ToString() + ")");
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region Start Auto Attack
        public bool AutoAttack()
        {
            if (!Me.IsAutoAttacking && Me.CurrentTarget.IsWithinMeleeRange)
            {
                Me.ToggleAttack();
                return true;
            }
            return false;
        }
        #endregion

        #region Helpers
        // Check if spell is on cooldown
        private bool Cooldown(string SpellName)
        {
            if (SpellManager.Spells.ContainsKey(SpellName) && SpellManager.Spells[SpellName].Cooldown) { return true; }
            return false;
        }

        // Check if we have learned a spell
        private bool HasSpell(string SpellName)
        {
            if (SpellManager.Spells.ContainsKey(SpellName)) { return true; }
            return false;
        }

        // Approach and face target
        private void ApproachAndFaceTarget()
        {
            if (!Me.CurrentTarget.IsWithinMeleeRange){ Styx.Pathing.Navigator.MoveTo(Me.CurrentTarget.Location); }
            if (Me.CurrentTarget.IsWithinMeleeRange) { WoWMovement.MoveStop(); WoWMovement.ConstantFace(Me.CurrentTarget.Guid); }
        }
        #endregion
    }
}
